package com.admin.service;

import com.admin.dao.AdminDao;
import com.admin.entity.*;
import com.admin.util.CommonResponse;
import com.admin.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    AdminDao adminDao;
    @Autowired
    TokenUtil tokenUtil;
    //管理员登录
    public CommonResponse adminLogin(Admin admin){
        List<Admin> selectAdmin = adminDao.selectUser(admin);
        if(!selectAdmin.isEmpty()){
            String s = tokenUtil.generateToken(selectAdmin.get(0).getId());
            return new CommonResponse(200,selectAdmin.get(0).getAdminName(),s);
        }
        else
            return new CommonResponse(500,"false");
    }

    //显示所有用户
    public CommonResponse userShow(){
        List<User> allUser = adminDao.userShow();
        return new CommonResponse(200,"ok",allUser);
    }

    //删除用户
    public CommonResponse userDelete(int id){
        if(adminDao.deleteUser(id) != 0)
            return new CommonResponse(200,"delete success!");
        else
            return new CommonResponse(500,"delete false!");
    }

    //显示订单
    public CommonResponse orderShow(){
        List<Order> allOrder = adminDao.orderShow();
        return new CommonResponse(200,"ok",allOrder);
    }

    //删除订单
    public CommonResponse orderDelete(int id){
        if (adminDao.deleteOrder(id) != 0)
            return new CommonResponse(200,"delete success!");
        else
            return new CommonResponse(500,"delete false!");
    }

    //显示库存量
    public CommonResponse repertoryShow(){
        List<RepositoryFinal> repositoryFinals = adminDao.repositorySelect();
        return new CommonResponse(200,"ok",repositoryFinals);
    }

    //修改库存量
    public CommonResponse repertoryManage(Repository repository){
        if(adminDao.repertoryUpdate(repository) != 0)
            return new CommonResponse(200,"修改成功！");
        else
            return new CommonResponse(500,"修改失败");
    }

    //产品查看
    public CommonResponse productShow(){
        List<Product> allProduct = adminDao.productSelect();
        return new CommonResponse(200,"ok",allProduct);
    }

    //产品删除
    public CommonResponse productDelete(int id){
        if(adminDao.productDelete(id) != 0)
            return new CommonResponse(200,"删除成功！");
        else
            return new CommonResponse(500,"删除失败");
    }

    //产品添加
    public CommonResponse productAdd(ProductFinal productFinal){
        if(adminDao.productInsert(productFinal) != 0){
            adminDao.repertoryAdd(productFinal);
            return new CommonResponse(200,"上架成功！");}
        else
            return new CommonResponse(500,"上架失败！");
    }
}
